package com.lam.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lam.dormitory.util.ShiroUtils;
import com.lam.dormitory.entity.Repair;
import com.lam.dormitory.entity.Student;
import com.lam.dormitory.service.IDormitoryService;
import com.lam.dormitory.service.IRepairService;
import com.lam.dormitory.service.IStudentService;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.RepairVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lam
 * @since 2019-12-26
 */
@Api(tags = "报修接口")
@RestController
@RequestMapping("/repair")
public class RepairController {
    @Autowired
    private IRepairService repairService;
    @Autowired
    private IDormitoryService dormitoryService;
    @Autowired
    private IStudentService studentService;

    /**
     * 添加报修申请
     *
     * @param repair
     * @return
     */
    @ApiOperation(value = "添加报修申请", notes = "添加报修申请")
    @PostMapping("/add")
    @RequiresRoles(value = {"admin", "superadmin", "student"}, logical = Logical.OR)
    public ResponseTemplate addRepair(Repair repair) {
        repair.setSubmitDate(new Date());
        repair.setStatus(1);
        boolean save = repairService.save(repair);
        if (save) {
            return new ResponseTemplate(200, "添加成功");
        }
        return new ResponseTemplate(500, "添加失败");
    }

    /**
     * 删除报修
     *
     * @param repairId
     * @return
     */
    @ApiOperation(value = "删除报修", notes = "根据repairId删除报修")
    @PostMapping("/delete")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate deleteRepair(Long repairId) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Repair::getRepairId, repairId);
        boolean remove = repairService.remove(wrapper);
        if (remove) {
            return new ResponseTemplate(200, "删除成功");
        }
        return new ResponseTemplate(500, "删除失败");
    }

    /**
     * 修改报修信息
     *
     * @param repair
     * @returns
     */
    @ApiOperation(value = "修改报修信息", notes = "可更新状态，根据repairId修改报修信息")
    @PostMapping("/edit")
    @RequiresRoles(value = {"admin", "superadmin", "student"}, logical = Logical.OR)
    public ResponseTemplate editRepair(Repair repair) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Repair::getRepairId, repair.getRepairId());
        boolean update = repairService.update(repair, wrapper);
        if (update) {
            return new ResponseTemplate(200, "修改成功");
        }
        return new ResponseTemplate(500, "修改失败");
    }

    /**
     * 报修信息列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "报修信息列表", notes = "可根据dorId 或者 repairId repairKind repairReason查询报修信息列表,pageNum 默认为 1，pageSize默认为 0")
    @PostMapping("/list")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin", "student"}, logical = Logical.OR)
    public ResponseTemplate pageRepair(Repair repair, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RepairVo> repairVoList = repairService.selectRepairVo(repair);
        PageInfo<RepairVo> pageInfo = new PageInfo<>(repairVoList);
        return new ResponseTemplate(200, "报修信息列表返回成功", repairVoList, pageInfo.getTotal());
    }

    /**
     * 根据当前学生userId查询报修服务信息
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "用户报修信息列表(学生userId)", notes = "可根据当前userId或者填入userId查询报修服务信息")
    @PostMapping("/student/list")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin", "student"}, logical = Logical.OR)
    public ResponseTemplate RepairByUserId(Long userId) {
        if (userId == null) {
            userId = ShiroUtils.getUserInfo().getUserId();
        }
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Student::getUserId, userId);
        Student student = studentService.getOne(wrapper);
        List<RepairVo> repairVoList = repairService.selectRepairVoByDorId(student.getDorId());
        return new ResponseTemplate(200, "查询成功", repairVoList);
    }
}
